package com.jltx.xq.simplewatch.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author jltxseo
 * @date 2017/08/29.
 * @description:
 */

public class BaseToolbar extends Toolbar{
    private OnDoubleClickListener onDoubleClickListener;
    private GestureDetector detector;

    public BaseToolbar(Context context) {
        super(context);
        initView(context);
    }

    public BaseToolbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public BaseToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public OnDoubleClickListener getOnDoubleClickListener() {
        return onDoubleClickListener;
    }

    public void setOnDoubleClickListener(OnDoubleClickListener onDoubleClickListener) {
        this.onDoubleClickListener = onDoubleClickListener;
    }

    private void initView(Context context){
        detector = new GestureDetector(context,new DoubleGestureDetector(this));
    }

    public interface OnDoubleClickListener {
        void onDoubleClick(View view,MotionEvent e);
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        detector.onTouchEvent(ev);
        return true;
    }

    private class DoubleGestureDetector extends GestureDetector.SimpleOnGestureListener{
        private View view;

        public DoubleGestureDetector(View view) {
            this.view = view;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) { //检测双击事件回调
            if(onDoubleClickListener != null)onDoubleClickListener.onDoubleClick(view,e);
            return true;
        }
    }


}
